package com.yami.shop.app.utils;

import java.security.MessageDigest;
import java.util.Arrays;

public class WechatUtil {

    private static final String token = "cjm1314520";

    public static boolean checkSignature(String signature,String timestamp,String nonce){

         //定义数组存放tooken，timestamp,nonce

         String[] arr = {token,timestamp,nonce};

         //对数组进行排序

         Arrays.sort(arr);

         //生成字符串

         StringBuffer sb = new StringBuffer();

         for(String s : arr){

            sb.append(s);

         }

         //sha1加密,网上均有现成代码

         String temp = getSha1(sb.toString());

         //将加密后的字符串，与微信传来的加密签名比较，返回结果
         System.out.println("signature="+signature+"??timestamp="+timestamp+"??nonce="+nonce);
         System.out.print("签名为"+signature+"加密后的密文为"+temp);
         return temp.equals(signature);

    }

    public static String getSha1(String str){

             if(str==null||str.length()==0){

                return null;

             }

             char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f'};

             try {

             MessageDigest mdTemp = MessageDigest.getInstance("SHA1");

             mdTemp.update(str.getBytes("UTF-8"));

             byte[] md = mdTemp.digest();

            int j = md.length;

             char buf[] = new char[j*2];

             int k = 0;

            for (int i = 0; i < j; i++) {

                byte byte0 = md[i];

                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];

                buf[k++] = hexDigits[byte0 & 0xf];
           }

             return new String(buf);

             } catch (Exception e) {

                 return null;
             }

    }




}
